package com.key.modules.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.key.common.base.RespResult;
import com.key.modules.dto.VehicleAddDTO;
import com.key.modules.dto.VehicleEditDTO;
import com.key.modules.entity.Vehicle;
import com.key.modules.mapper.CarrierMapper;
import com.key.modules.mapper.VehicleMapper;
import com.key.modules.service.VehicleService;
import com.key.modules.vo.VehicleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author wei
* @description 针对表【vehicle(车辆表)】的数据库操作Service实现
* @createDate 2023-03-23 19:32:31
*/
@Service
@Slf4j
public class VehicleServiceImpl implements VehicleService{

    @Autowired
    CarrierMapper carrierMapper;

    @Autowired
    VehicleMapper vehicleMapper;

    @Override
    public RespResult findVehicleList(Integer currentPage, Integer pageSize,
                                      String vNumber, String vType, String cName) {
        log.info("进入findVehicleList方法，查询车辆表（支持条件查询）"+
                "，currentPage = "+currentPage+
                "，pageSize = "+pageSize+
                "，vNumber = "+vNumber+
                "，vType = "+vType+
                "，cName = "+cName);

        Integer cId = carrierMapper.selectCIdByCName(cName);

        Page<Vehicle> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<Vehicle> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(vNumber), Vehicle::getVNumber, vNumber)
                .eq(StringUtils.isNotEmpty(vType), Vehicle::getVType, vType)
                .eq(cId != null, Vehicle::getCId, cId);

        Page<Vehicle> vehiclePage = vehicleMapper.selectPage(page, lambdaQueryWrapper);

        List<Vehicle> vehicleList = vehiclePage.getRecords();

        List<VehicleVO> vehicleVOList = new ArrayList<>();
        for(Vehicle vehicle : vehicleList){
            VehicleVO vehicleListVO = BeanUtil.copyProperties(vehicle, VehicleVO.class);
            vehicleListVO.setCName(carrierMapper.selectCNameByCId(vehicle.getCId()));
            vehicleVOList.add(vehicleListVO);
        }

        log.info("findVehicleList方法结束");
        return RespResult.success((int) vehiclePage.getTotal(), vehicleVOList);
    }

    @Override
    public boolean addVehicle(VehicleAddDTO vehicleAddDTO) {
        log.info("进入addVehicle方法，添加车辆"+
                "，vehicleAddDTO = "+vehicleAddDTO);

        Vehicle vehicle = BeanUtil.copyProperties(vehicleAddDTO, Vehicle.class);
        vehicle.setCId(carrierMapper.selectCIdByCName(vehicleAddDTO.getCName()));
        int result = vehicleMapper.insert(vehicle);

        log.info("addVehicle方法结束");
        return result > 0;
    }

    @Override
    public boolean removeVehicle(Integer vId) {
        log.info("进入removeVehicle方法，删除车辆）"+
                "，vId = "+vId);

        int result = vehicleMapper.deleteById(vId);

        log.info("removeVehicle方法结束");
        return result > 0;
    }

    @Override
    public boolean editVehicle(VehicleEditDTO vehicleEditDTO) {
        log.info("进入editVehicle方法，修改车辆"+
                "，vehicleEditDTO = "+vehicleEditDTO);

        Vehicle vehicle = BeanUtil.copyProperties(vehicleEditDTO, Vehicle.class);
        vehicle.setCId(carrierMapper.selectCIdByCName(vehicleEditDTO.getCName()));
        int result = vehicleMapper.updateById(vehicle);

        log.info("editVehicle方法结束");
        return result > 0;
    }

}




